package org.makeprogress.springbootbase.syserrorhandler;

import org.makeprogress.springbootbase.model.Result;
import org.springframework.validation.BindException;
import org.springframework.validation.FieldError;
import org.springframework.web.HttpMediaTypeNotSupportedException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.MissingServletRequestParameterException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import javax.validation.ConstraintViolationException;
import java.util.stream.Collectors;

/**
 * @author liuyi
 * @create 2019-10-09 20:50
 **/
@RestControllerAdvice
public class AppExceptionHandler {
    /**
     * 处理不带任何注解的参数绑定校验异常
     *
     * @param e
     * @return
     */
    @ExceptionHandler(BindException.class)
    public Result<String> handleBingException(BindException e) {
        e.printStackTrace();
        String errorMsg = e.getBindingResult().getAllErrors()
                .stream()
                .map(objectError -> ((FieldError) objectError).getField() + ((FieldError) objectError).getDefaultMessage())
                .collect(Collectors.joining(","));
        Result<String> result = new Result<>();
        result.setMsg(errorMsg);
        result.setCode(500);
        return result;
    }

    /**
     * 处理 @RequestBody参数校验异常
     *
     * @param e
     * @return
     */
    @ExceptionHandler(MethodArgumentNotValidException.class)
    public Result<String> handleMethodArgumentNotValidException(MethodArgumentNotValidException e) {
        String errorMsg = e.getBindingResult().getAllErrors()
                .stream()
                .map(objectError -> ((FieldError) objectError).getField() + ((FieldError) objectError).getDefaultMessage())
                .collect(Collectors.joining(","));
        Result<String> result = new Result<>();
        result.setMsg(errorMsg);
        result.setCode(500);
        return result;
    }

    /**
     * 处理 @RequestBody参数校验异常
     *
     * @param e
     * @return
     */
    @ExceptionHandler(ConstraintViolationException.class)
    public Result<String> handleValidationExceptionException(ConstraintViolationException e) {
        Result<String> result = new Result<>();
        String errorMsg = e.getMessage();
        result.setMsg(errorMsg);
        result.setCode(500);
        return result;
    }


    /**
     * 处理请求参数没有传递的情况下
     *
     * @param e
     * @return
     */
    @ExceptionHandler(MissingServletRequestParameterException.class)
    public Result<String> handleMissingServletRequestParameterException(MissingServletRequestParameterException e) {
        Result<String> result = new Result<>();
        String errorMsg = e.getMessage();
        result.setMsg(errorMsg);
        result.setCode(500);
        return result;
    }


    /**
     * 处理请求参数没有传递的情况下
     *
     * @param e
     * @return
     */
    @ExceptionHandler(HttpMediaTypeNotSupportedException.class)
    public Result<String> handleHttpMediaTypeNotSupportedException(HttpMediaTypeNotSupportedException e) {
        Result<String> result = new Result<>();
        String errorMsg = e.getMessage();
        result.setMsg(errorMsg);
        result.setCode(500);
        return result;
    }

}